package com.hy.controller;

import com.hy.bean.Consumer;
import com.hy.bean.RespBean;
import com.hy.service.impl.ConsumerServiceImpl;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * (Consumer)表控制层
 *
 * @author makejava
 * @since 2024-04-29 11:08:28
 */
@RestController
@CrossOrigin
@RequestMapping("consumer")
public class ConsumerController {
    /**
     * 服务对象
     */

    private ConsumerServiceImpl consumerService = new ConsumerServiceImpl();

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Consumer> queryById(@PathVariable("id")int id) {
        return ResponseEntity.ok(this.consumerService.queryById(id));
    }

    @GetMapping("queryAll")
    public List<Consumer> queryAll(){
        return consumerService.queryAll();
    }

    /**
     * 新增数据
     *
     * @param consumer 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<Consumer> add(Consumer consumer) {
        return ResponseEntity.ok(this.consumerService.insert(consumer));
    }

    /**
     * 编辑数据
     *
     * @param consumer 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<Consumer> edit(Consumer consumer) {
        return ResponseEntity.ok(this.consumerService.update(consumer));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(int id) {
        return ResponseEntity.ok(this.consumerService.deleteById(id));
    }


    @RequestMapping("queryConsumerBy")
    @ResponseBody
    public RespBean queryConsumerBy(String username,String pwd, HttpServletRequest req, HttpServletResponse resp){
        return consumerService.getConsumer(username, pwd, req, resp);
    }

}

